package com.example.rydwlib

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.baselib.BaseViewModel
import com.example.baselib.constants.MyConstants
import com.example.baselib.databean.UserData
import com.example.rydwlib.bean.RyNickBean
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

/**
 * Author: sxygsj
 * Date: 2020/8/25 9:51
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class RydwAllViewModel (application: Application) : BaseViewModel(application)  {
    private val repository = RydwRepository()
    private val userData: UserData = MyConstants.userData
    private val allNickData: MutableLiveData<List<RyNickBean>> by lazy { MutableLiveData<List<RyNickBean>>().also { loadRydwNickData() } }

    fun getRydwNickData(): LiveData<List<RyNickBean>> {
        return  allNickData
    }
    fun loadRydwNickData()=launchUI {
        val result = repository.loadRydwNickData(userData.token)
        val type = object : TypeToken<List<RyNickBean>>() {}.type
        allNickData.value = Gson().fromJson<List<RyNickBean>>(result.body()?.data, type)
    }
}